package cn.yunhe.menu;

import java.util.Date;
import java.util.List;
import java.util.Scanner;

import cn.yunhe.dao.BoardDao;
import cn.yunhe.dao.ReplyDao;
import cn.yunhe.dao.TopicDao;
import cn.yunhe.dao.UserDao;
import cn.yunhe.dao.impl.BoardDaoImpl;
import cn.yunhe.dao.impl.ReplyDaoImpl;
import cn.yunhe.dao.impl.TopicDaoImpl;
import cn.yunhe.dao.impl.UserDaoImpl;
import cn.yunhe.entity.Board;
import cn.yunhe.entity.Reply;
import cn.yunhe.entity.Topic;
import cn.yunhe.entity.User;
import cn.yunhe.util.DateUtil;

public class MenuFactory {
	private static UserDao userDao = new UserDaoImpl();
	private static BoardDao boardDao = new BoardDaoImpl();
	private static TopicDao topicDao = new TopicDaoImpl();
	private static ReplyDao replyDao = new ReplyDaoImpl();
	private static Scanner input = new Scanner(System.in);
	private static User currentUser; // 标记 当前 登录的用户 信息

	/**
	 * 用户/管理员 登录
	 */
	public static void loginMenu() {
		System.out.println("********************欢迎访问BBS论坛*************************");
		System.out.println("1.登录  2.注册");
		System.out.println("*********************************************************");
		System.out.println("请选择:");
		String choice = input.next();
		switch (choice) {
		case "1": // 登录
			User user = null;
			while (true) {
				System.out.println("请输入用户名:");
				String loginName = input.next();
				System.out.println("请输入密码:");
				String pwd = input.next();
				user = userDao.login(loginName, pwd);
				if (user != null) {
					System.out.println("登录成功!!");
					currentUser = user;
					break;
				} else {
					System.out.println("用户名或密码错误");
				}
			}
			if (user.getFlag() == 0) {// 管理员
				showAminMainMenu();
			} else {// 普通用户
				showUserMainMenu();
			}
			break;
		case "2": // 注册
			System.out.println("请输入账号:");
			String uname = input.next();
			System.out.println("请输入密码:");
			String pwd = input.next();
			boolean result = userDao.register(new User(uname, pwd, 0, 1));
			if (result) {
				System.out.println("注册成功!!");
			} else {
				System.out.println("注册失败!!");
			}
			break;
		default:
			System.out.println("输入选项无效");
			break;
		}

	}

	/**
	 * 管理员主菜单
	 */
	public static void showAminMainMenu() {
		System.out.println("**********************************");
		System.out.println("1.查看板块  2.添加板块 3.查看用户 4.禁用用户  5.解禁用户");
		System.out.println("请选择：");
		String choice = input.next();
		switch (choice) {
		case "1":
			List<Board> blist = boardDao.findAllBoard();
			if (blist != null) {
				for (Board board : blist) {
					System.out.println(board.getId() + "\t" + board.getName());
				}
			} else {
				System.out.println("暂无版块数据");
			}
			break;
		case "2":
			System.out.println("请输入新版块名称");
			String bname = input.next();
			boolean result = boardDao.addBoard(new Board(bname));
			if (result) {
				System.out.println("版块添加成功!!!");
			} else {
				System.out.println("版块添加失败!!!");
			}
			break;
		case "3":
			List<User> ulist = userDao.findAllUser();
			if (ulist != null) {
				System.out.println("编号\t账号\t状态\t角色");
				for (User user : ulist) {
					System.out.print(user.getId() + "\t" + user.getLoginName() + "\t");
					if (user.getState() == 0) {
						System.out.print("正常\t");
					} else {
						System.out.print("禁用\t");
					}
					if (user.getFlag() == 0) {
						System.out.print("管理员\t");
					} else {
						System.out.print("普通用户\t");
					}
					System.out.println();
				}
			}
			break;
		case "4":
			System.out.println("请输入要冻结用户的id:");
			userDao.lockUnlockUser(input.nextInt(), true);
			break;
		case "5":
			System.out.println("请输入要冻结用户的id:");
			userDao.lockUnlockUser(input.nextInt(), false);
			break;
		default:
			break;
		}

	}

	/**
	 * 普通用户主菜单
	 */
	public static void showUserMainMenu() {
		while (true) {
			System.out.println("************************************");
			System.out.println("1.选择版块  2.查看我的发帖  3.查看我参与回帖的主帖");
			System.out.println("请选择:");
			String choice = input.next();
			switch (choice) {
			case "1":
				showAndSelectBoard();// 显示所有版块并选择
				break;
			case "2":
                showTopicOfMine();
				break;
			case "3":
                showTopicOfMyReplies();
				break;
			default:
				break;
			}
		}
	}

	/**
	 * 版块选择
	 */
	public static void showAndSelectBoard() {
		while (true) {
			List<Board> blist = boardDao.findAllBoard();
			if (blist != null) {
				for (Board board : blist) {
					System.out.println(board.getId() + "\t" + board.getName());
				}
				System.out.println("请选版块:");
				int bid = input.nextInt();

				showMenuUnderOneBoard(bid);

			} else {
				System.out.println("暂无版块数据");
			}
		}

	}

	/**
	 * 打印显示 主帖集合
	 * 
	 * @param tlist
	 *            要展示的 主帖集合
	 */
	public static void showTopics(List<Topic> tlist) {
		if (tlist != null) {
			for (Topic topic : tlist) {
				System.out.println("编号:" + topic.getId());
				System.out.println("标题:《" + topic.getTitle() + "》");
				System.out.println("发帖时间:" + topic.getCreateTime());
				System.out.println("---------------------------------------------------");
			}

		} else {
			System.out.println("暂无数据");
		}
	}

	/**
	 * 显示 选定版块 后的 菜单
	 * 
	 * @param bid
	 */
	public static void showMenuUnderOneBoard(int bid) {
		while (true) {
			System.out.println("1.看帖   2.发帖 3.返回上一级");
			System.out.println("请输入:");
			String choice = input.next();
			// System.out.println(choice + "##################");
			switch (choice) {
			case "1":// 看帖,查看帖子详情
				showAllTopicsOfSelectedBoard(bid);
				break;
			case "2":// 发帖
				System.out.println("请输入标题");
				String title = input.next();
				System.out.println("请输入内容");
				String content = input.next();
				Topic topic = new Topic(title, content, DateUtil.dateTimeToString(new Date()));
				topic.setBoard(new Board(bid));
				topic.setUser(new User(currentUser.getId()));
				boolean result = topicDao.addTopic(topic);
				if (result) {
					System.out.println("发帖成功");
				} else {
					System.out.println("发帖失败!!");
				}
			case "3":// 返回上一级
				return;
			}
		}

	}

	/**
	 * 打印主帖详情(主帖内容, 回帖)
	 */
	public static void showTopicDetail(Topic topic) {

		System.out.println("标题:"+topic.getTitle());
		System.out.println("内容:"+topic.getContent());
		List<Reply> rlist = replyDao.findReplyByTopicId(topic.getId());
		for (int i = 0; i < rlist.size(); i++) {// 遍历当前主帖的所有回帖
			Reply reply = rlist.get(i);
			System.out.println("★★★" + (i + 1) + "楼★★★");
			System.out.println("回帖内容:" + reply.getContent());
			System.out.println("回帖人:" + reply.getUser().getLoginName());
			System.out.println("回帖时间:" + reply.getCreateTime());
			System.out.println("-----------------------------------------------");
		}

	}

	/**
	 * 回帖
	 * 
	 * @param topic
	 */
	public static void addReply(Topic topic) {
		System.out.println("请输入回帖内容:");
		String content = input.next();
		Reply reply = new Reply(content, DateUtil.dateTimeToString(new Date()), currentUser, topic);
		boolean result = replyDao.addReply(reply);
		if (result) {
			System.out.println("回帖成功!!");
		} else {
			System.out.println("回帖失败!!");
		}
	}

	/**
	 * 查看 选定版块 下的所有 主帖
	 * 
	 * @param bid
	 */
	public static void showAllTopicsOfSelectedBoard(int bid) {
		while (true) {
			Board b = boardDao.getBoardById(bid);
			System.out.println("当前所在版块:" + b.getName());
			List<Topic> tlist1 = topicDao.findTopicsByBoardId(bid);
			showTopics(tlist1);
			System.out.println("请选择主帖:");
			int tid = input.nextInt();
			Topic topicDetail = topicDao.getTopicByTid(tid);

			topicDetailMenu(topicDetail);
		}

	}

	/**
	 * 针对于某个主帖的 菜单选项
	 * 
	 * @param topicDetail
	 *            主帖详情
	 */
	public static void topicDetailMenu(Topic topicDetail) {
		while (true) {
			showTopicDetail(topicDetail); // 打印主帖详情
			System.out.println("1.返回上一级  2.回帖");
			String choice2 = input.next();
			switch (choice2) {
			case "1":
				return;
			case "2":
				addReply(topicDetail);
				break;
			default:
				System.out.println("选项无效 ");
				break;
			}

		}

	}
    /**
     * 查看当前用户 所发过的主帖
     */
	public static void showTopicOfMine() {
		while(true){
			List<Topic> tlist = topicDao.findTopicsByUserId(currentUser.getId());
			showTopics(tlist);
			System.out.println("1.返回上一级  2.删帖");
			System.out.println("请选择:");
			String choice = input.next();
			switch (choice) {
			case "1":
	            return;
			case "2":
                System.out.println("请选择要删的 主帖 编号:");
                int tid = input.nextInt();
                boolean result = topicDao.deleteTopic(tid,currentUser.getId());
                if(result){
                	System.out.println("删除成!!");
                }else{
                	System.out.println("删除失败!!");
                }
				break;
			default:
				break;
			}
		}
	}
	
	public  static void  showTopicOfMyReplies(){
		List<Topic> tlist = topicDao.findTopicsByReplyUserId(currentUser.getId());
		showTopics(tlist);
	}

}
